//
//  SecondViewController.m
//  AutoLayoutDemo
//
//  Created by mengxiang on 5/30/15.
//  Copyright (c) 2015 mengxiang. All rights reserved.
//

#import "SecondViewController.h"

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.layer.borderWidth = 2.0;
    leftButton.layer.borderColor = [UIColor blackColor].CGColor;
    [leftButton setTitle:@"左上角按钮" forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.layer.borderWidth = 2.0;
    rightButton.layer.borderColor = [UIColor blackColor].CGColor;
    [rightButton setTitle:@"右上角按钮" forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeSystem];
    bottomButton.layer.borderWidth = 2.0;
    bottomButton.layer.borderColor = [UIColor blackColor].CGColor;
    [bottomButton setTitle:@"底部按钮" forState:UIControlStateNormal];
    [self.view addSubview:bottomButton];
    
    leftButton.translatesAutoresizingMaskIntoConstraints = NO;
    rightButton.translatesAutoresizingMaskIntoConstraints = NO;
    bottomButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    // H|V:表示横竖方向
    // ｜:表示superview
    //［］用来标示约束的对象
    //（）里定义约束对象的属性如宽高，priority等
    // - 代表space，如－80-是两者间隔为80，>=80即最小间隔是80
    
    BOOL useMetrics = YES;
    NSMutableArray *constraintsList = [NSMutableArray array];
    if (useMetrics) {
        [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-80-[leftButton(==100)]-40-[rightButton(==100)]-80-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(leftButton, rightButton)]];
        [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[leftButton(==30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(leftButton)]];
        [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[rightButton(==leftButton)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(rightButton, leftButton)]];
    } else {
        NSDictionary *metricMap = @{@"width":@100.0, @"height":@30.0, @"leading":@80, @"top":@30.0};
        [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(leading)-[leftButton(width)]-[rightButton(leftButton)]-(leading)-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metricMap views:NSDictionaryOfVariableBindings(rightButton, leftButton)]];
        [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(top)-[rightButton(height)]" options:0 metrics:metricMap views:NSDictionaryOfVariableBindings(rightButton, leftButton)]];
    }
    [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-60-[bottomButton]-60-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bottomButton)]];
    [constraintsList addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomButton(==30)]-50-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(bottomButton)]];
    
    [self.view addConstraints:constraintsList];
}

@end
